<template>
  <div class="LighProfitbox">
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="80px">
          <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="光伏系统">
                  <FilterSelect2
                      v-model="companyId"
                      :optionlist="xitonglist"
                      @updateModelValue="changexitong"
                      :valuekey="{ name: 'name', value: 'id' }"
                    ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="8" style="display: flex;margin-top: 4px;">
                <el-radio-group v-model="radio3" size="small" style="margin-right: 20px;">
                  <el-radio-button label="1">日</el-radio-button>
                  <el-radio-button label="2">月</el-radio-button>
                  <el-radio-button label="3">年</el-radio-button>
                  <el-radio-button label="4">总</el-radio-button>
                </el-radio-group>
                <el-date-picker style="flex:1;" v-if="radio3=='1'" v-model="value1" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" size="small"></el-date-picker>
                <el-date-picker style="flex:1;" v-if="radio3=='2'" v-model="value2" type="month" value-format="yyyy-MM" placeholder="选择月" size="small"></el-date-picker>
                <el-date-picker style="flex:1;" v-if="radio3=='3'" v-model="value3" type="year" value-format="yyyy" placeholder="选择年" size="small"></el-date-picker>
                <el-date-picker style="flex:1;" v-if="radio3=='4'" v-model="value4" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" size="small"></el-date-picker>
              </el-col>
              <el-col :span="11" style="text-align: right;">
                <el-form-item label="">
                  <el-button size="small" @click="chongzhi">重 置</el-button>
                  <el-button size="small" type="primary" @click="gettablelist">查 询</el-button>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="title">
        <div>光伏收益分析</div>
        <div><TopTile  @command="command" @daochu="daochu" @shuaxin="shuaxin"></TopTile></div>
      </div>
      <div class="echandtab">
        <div class="tongbiech" ref="tongbiech"></div>
        <div class="tongbiech">
          <TableColumn
                ref="tabledatadianliu"
                :tableData="tableData" 
                :tableConfig="tableConfig" 
                :issize="issize">
              </TableColumn>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import {getshouyiquxianapi,getshouyitableapi} from '@/api/Photovoltaic/settlement/LighProfit'
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import {exportexcel} from '@/utils/excel'
import {getphotovollistapi} from '@/api/Photovoltaic/analysis/YoyAnalysis'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn
  },
  data() {
    return {
      radio3:'1',
      value1:'',
      value2:'',
      value3: '',
      value4: '',
      tableData: [],
      tableConfig: [
      { label: '序号', type: 'index', center: 'center', width: '70' },
        {label:'时间',prop:'dateString',center: 'center', width: '170'},
        { label: '当前发电量(kWh)', prop: 'currentPower' ,center: 'right'},
        { label: '自发自用电量(kWh)', prop: 'solarPower' ,center: 'right'},
        { label: '自发自用收益(元)', prop: 'selfPowerProfit',center: 'right' },
        { label: '上网电量(kWh)', prop: 'netPower',center: 'right' },
        { label: '上网收益(元)', prop: 'netPowerProfit' ,center: 'right'},
        { label: '当前总收益(元)', prop: 'totalProfit' ,center: 'right'},
      ],
      issize: 'medium',
      companyId: '',
      xitonglist:[]
    };
  },

  async mounted() {
    this.gettime()
    const { data: { list } } = await getphotovollistapi()
    this.xitonglist = list
    const PhotoCompanyId = localStorage.getItem('PhotoCompanyId')
    this.$set(this, 'companyId', PhotoCompanyId ? +PhotoCompanyId : list[0].id)
    this.gettablelist()
  },

  methods: {
    async gettablelist() {
      this.getqushitubiao()
      //获取收益表格数据
      this.$refs.tabledatadianliu.loading=true
      const { data: { list } } = await getshouyitableapi({
        dateString: this.radio3 == '1' ? this.value1 : this.radio3 == '2' ? this.value2 : this.radio3 == '3' ? this.value3 : this.value4,
        dateType: this.radio3,classId: this.companyId,
      })
      this.tableData=list
      this.$refs.tabledatadianliu.loading=false
    },
    chongzhi() { 
      this.radio3 = '1'
      this.gettime()
      this.gettablelist()
    },
    // 获取收益曲线
    async getqushitubiao() {
      var chartDom = this.$refs.tongbiech
      var myChart = echarts.init(chartDom);
      var option;
      const loading = this.$loading({
        lock: true,
        text: 'loading',
        spinner: 'el-icon-loading',
        target: chartDom
      });
      const { data: { list } } = await getshouyiquxianapi({
         dateString: this.radio3 == '1' ? this.value1 : this.radio3 == '2' ? this.value2 : this.radio3 == '3' ? this.value3 : this.value4,
        dateType: this.radio3,classId: this.companyId,
      })
      const timePoints =[]
      for (let i = 0; i < list.length; i++) {
          if (list[i].datalist.length > 0) {
              list[i].datalist.forEach(data => {
                timePoints.push(data.timeReading);
              });
              break;
          }
      }
      option = {
        color:['#5087ec','#68bbc4','#58a55c','#f2bd42'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        grid: {
          left: '2%',
          right: '1%',
          bottom: '3%',
          containLabel: true
        },
        legend: {},
        xAxis: [
          {
            type: 'category',
            data: timePoints,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            // name: '电量(kWh)',
            // axisLine: {
            //   show: true
            // }
          },
          {
            type: 'value',
            // name: 'PR(%)',
            // axisLine: {
            //   show: true
            // }
          }
        ],
        series: [
          {
            name: '发电量',
            type: 'bar',
            data: list.filter(ele=>ele.name=='发电量')[0].datalist.map(ele=>ele.valueReading)
          },
          {
            name: '自发自用',
            type: 'bar',
            data: list.filter(ele=>ele.name=='自发自用')[0].datalist.map(ele=>ele.valueReading)
          },
          {
            name: '上网电量',
            type: 'bar',
            data: list.filter(ele=>ele.name=='上网电量')[0].datalist.map(ele=>ele.valueReading)
          },
          {
            name: '总收益',
            type: 'line',
            yAxisIndex: 1,
            data:list.filter(ele=>ele.name=='总收益')[0].datalist.map(ele=>ele.valueReading)
          }
        ]
      };
      myChart.clear();
      option && myChart.setOption(option);
      loading.close()
    },
    // 系统下拉存本地
    changexitong(val) {
      localStorage.setItem('PhotoCompanyId', val)
    },
        // 调节表格密度
    command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 导出
    daochu() {
      exportexcel('pes-microservice-biz/pes/photovoltaic/settlement/benefit/analysis/exportExcel',{
        dateString: this.radio3 == '1' ? this.value1 : this.radio3 == '2' ? this.value2 : this.radio3 == '3' ? this.value3 : this.value4,
        dateType: this.radio3,classId: this.companyId,
      },'光伏收益')
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    },
    // 获取当前时间戳
    gettime() {
      const date = new Date()
      const year = date.getFullYear() // 年
      const mon = date.getMonth() + 1 // 月
      const month = mon < 10 ? '0' + mon : mon
      const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() //日
      this.$set(this,'value1',year+'-'+month+'-'+day)
      this.$set(this,'value2',year+'-'+month)
      this.$set(this, 'value3', year.toString())
      this.value4=''
    },
  },
};
</script>

<style scoped>
.LighProfitbox{
  height: 100%;
  padding: 10px;
  .topbox{
    height: 80px;
    background-color: #fff;
    padding: 20px;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    background-color: #fff;
    padding: 20px;
    .title{
      margin-bottom: 10px;
      display:flex;
      justify-content:space-between;
    }
    .echandtab{
      height: 95%;
      .tongbiech{
        height: 50%;
      }
    }
  }
}
</style>